from rest_framework import serializers

from Headlines.models import Article
from articles.serializers import UsersimpleSerializer
from question.models import Label, Question, Reply
from users.models import User


class LabelsimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = ('id', 'label_name')
class QuestionSerializerForList(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(read_only=True,many=True)

    class Meta:
        model = Question
        fields = ["id","createtime","labels","reply","replyname","replytime","title","unuseful_count","useful_count","user","visits"]
class ArticleSerializerForList(serializers.ModelSerializer):
    user = UsersimpleSerializer(read_only=True)
    collected = serializers.BooleanField(default=False)

    class Meta:
        model = Article
        fields = ("id", "title","content","createtime","user","collected_users","collected","image","visits")
class LabelSerializerWithQuestionAndArticle(serializers.ModelSerializer):
    questions = QuestionSerializerForList(many=True, read_only=True)
    articles = ArticleSerializerForList(many=True, read_only=True)
    users=UsersimpleSerializer(many=True)
    class Meta:
        model = Label
        fields = ['id','questions','articles','label_name','desc','baike_url','label_icon','users']

class QuestionSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(read_only=True, many=True)
    class Meta:
        model = Question
        fields = ["id","createtime","labels","reply","replyname","replytime","title","unuseful_count","useful_count","user","visits"]
class UserSerializerSimple(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar')
class ReplySerializerForSubAndParent(serializers.ModelSerializer):
    user = UserSerializerSimple(read_only=True)
    class Meta:
        model = Reply
        fields = ["id", "content","createtime","useful_count","unuseful_count","user"]

class ReplySerializerForList(serializers.ModelSerializer):

    user = UserSerializerSimple(read_only=True)
    subs = ReplySerializerForSubAndParent(read_only=True, many=True)
    parent =ReplySerializerForSubAndParent (read_only=True,default=None)
    class Meta:
        model = Reply
        fields = ["id", "content","createtime","useful_count",'problem',"unuseful_count","subs","user","parent"]
class ReplySerializerForList1(serializers.ModelSerializer):

    user = UserSerializerSimple(read_only=True)
    subs = ReplySerializerForSubAndParent(read_only=True, many=True)
    parent = ReplySerializerForSubAndParent(read_only=True,default=None)

    class Meta:
        model = Reply
        fields = ["id", "content","createtime","useful_count",'problem',"unuseful_count","subs","user","parent"]
class QuestionSerializerForDetail(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(read_only=True, many=True)
    comment_question = ReplySerializerForList(read_only=True, many=True)
    answer_question = ReplySerializerForList1(read_only=True, many=True)
    class Meta:
        model = Question
        fields = ["id","createtime","labels","reply","replyname","replytime","title","unuseful_count","useful_count","user","visits","content","comment_question","answer_question"]

class RelySerializers(serializers.ModelSerializer):
    class Meta:
        model = Reply
        fields = '__all__'



